package com.example.personal.mvvm.activity

import android.view.View
import android.widget.TextView
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.RegexUtils
import com.example.personal.R
import com.example.personal.mvvm.viewmodel.AccountViewModel
import com.gsafety.libraryktx.base.BaseActivity
import com.gsafety.libraryktx.bean.LoadState
import com.gsafety.libraryktx.core.RouterConstants.ACCOUNT_ACTIVITY
import com.gsafety.libraryktx.expand.create
import com.gsafety.libraryktx.expand.toast
import com.gsafety.libraryktx.manager.UserManager
import kotlinx.android.synthetic.main.personal_account.*
import kotlinx.android.synthetic.main.personal_account.bt_sure

@Route(path = ACCOUNT_ACTIVITY)
class AccountActivity : BaseActivity(),View.OnClickListener{
    private lateinit var mv: AccountViewModel
     var  userId:String? =""
    override fun initView() {
        var title = findViewById<TextView>(R.id.title_name)
        title.text = resources.getText(R.string.personal_my_information)
        var user=UserManager.getInstance().getUser()
        user?.let {
            tv_name.text=it.userName
            tv_company.text=it.orgName
            et_mobile.setText(it.phone)
            et_email.setText(it.email)
            userId=it.userId
        }
        initmodel()

    }

    override fun initData() {
        bt_sure.setOnClickListener(this)
    }

    override fun getLayoutId(): Int {
        return R.layout.personal_account
    }



    /**
     * 初始化viewmodel
     */
    private fun initmodel() {
        mv = AccountViewModel()
        mv = create(AccountViewModel::class)
        mv.loadState.observe(this, Observer {
            when (it) {
                is LoadState.Success -> {
                    disDia()
                    var login=UserManager.getInstance().getUser()
                    login?.let {
                        var moblie = et_mobile.text.toString().trim()
                        var email = et_email.text.toString().trim()
                        login.phone=moblie
                        login.email=email
                        UserManager.getInstance().putLogin(login)
                    }
                    toast("修改成功")
                    finish()
                }
                is LoadState.Fail -> {
                    disDia()
                    toast(it.msg)
                }
            }
        })
    }

    //逻辑判断
    fun judge(): Boolean {
        var moblie = et_mobile.text.toString().trim()
        var email = et_email.text.toString().trim()
        if (!RegexUtils.isMobileExact(moblie)) {
            toast("请输入正确格式的手机号")
            return false
        }
        if (!RegexUtils.isEmail(email)) {
            toast("请输入正确格式的邮箱")
            return false
        }
        return true

    }
    override fun onClick(v: View) {
        when(v.id){
            R.id.bt_sure->{
                if(judge()){
                    showDia("")
                    var moblie = et_mobile.text.toString().trim()
                    var email = et_email.text.toString().trim()
                    mv.updateUserInfo(moblie,email)
                }
            }
        }
    }

}